var webpack = require('webpack');

var glob = require('glob');

var path = require('path');

var fs = require('fs');
//build时拷贝资源文件
var CopyWebpackPlugin = require('copy-webpack-plugin');

//分离scss文件
var ExtractTextPlugin = require("extract-text-webpack-plugin");

//提取公用代码
var CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');

//加载html模板引擎
var HtmlWebpackPlugin = require('html-webpack-plugin');

//返回文件路径的map数组
var entries = function(globPath) {
    var files = glob.sync(globPath);
    var entriesObj= {}, entry, dirname, basename;

    for (var i = 0; i < files.length; i++) {
        entry = files[i];
        dirname = path.dirname(entry.replace(entry.split('/')[0] + '/', ''));
        basename = path.basename(entry, '.js');
        entriesObj[path.join(dirname, basename)] = './' + entry;
    }
    entriesObj.vendor = [__dirname+'/src/lib/ejs.min.js'];
    return entriesObj;
};

//组织每个页面的chunks
var htmlchunk = function(src) {
    var files = glob.sync(src);
    var template, filename;
    for (var i = 0; i < files.length; i++) {
        template = files[i];
        filename = path.dirname(template.replace(template.split('/')[0] + '/', '')) + '/index';
        configObj.plugins.push(new HtmlWebpackPlugin({
            filename: filename + '.html',
            template: template,
            inject:'head',
            chunks: [filename,'vendor']
        }));
    }
    return configObj.plugins;
};

var configObj = {
    entry:entries('src/**/*.js'),
    output: {
        path: path.join(__dirname, 'build/'),
        publicPath: '/build/',
        filename: '[name].entry.js',
        chunkFilename:'chunk/[name].chunk.js'
    },
    module: {
        loaders: [
            {
                test: /\.css$/,
                  loader:  "style-loader!css-loader"
            },
            {
                test: /\.s(a|c)ss$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    //resolve-url-loader may be chained before sass-loader if necessary
                    use: ['css-loader', 'sass-loader']
                })
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: path.join(__dirname, 'src/**/*.js'),
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'stage-0','imports-loader','html-loader']
                }
            },
            {   test: /\.ejs$/,
                loader: 'html-loader'
            }
        ]
    },
    plugins:[
            //生成对应的css文件
            new ExtractTextPlugin('[name].css'),
            //之拷贝images的图片资源文件
            new CopyWebpackPlugin([
                { from:__dirname + '/src/images/',to:__dirname + '/build/images/'}
            ]),
            new CommonsChunkPlugin({ names: ['vendor'], filename: '[name].js',minChunks: 2})
        ]
};

configObj.plugins = htmlchunk('src/pages/**/*.html');
//添加判断——只在开发模式下打开source-map
if (process.env.NODE_ENV == 'dev'){
    configObj.devtool = 'source-map'
}
module.exports = configObj;
